package edu.tongji.jiaowu.dao.jdbc;

import java.util.ArrayList;
import java.util.List;

import edu.tongji.jiaowu.dao.ExternalExamTypeDao;
import com.tongji.quickCRUDQ.define.Row;
import com.tongji.quickCRUDQ.model.CommonBean;
import com.tongji.quickCRUDQ.dao.jdbc.BaseDaoJdbc;

public class ExternalExamTypeDaoJdbc extends BaseDaoJdbc implements ExternalExamTypeDao {
	String tableName = "T_EXTERNAL_EXAM_TYPE";

	String beanName = "externalExamType";

	public CommonBean createExternalExamType() {
		CommonBean externalExamTypes = this.createBean(tableName, beanName);
		return externalExamTypes;
	}

	/*
	 public CommonBean findExternalExamTypes(String refid) {
	 CommonBean externalExamTypes=this.createExternalExamType();
	 String sql="select * from T_EXTERNAL_EXAM_TYPE where refid=?";
	 List data=this.getCommonDao().select(sql,new String[]{refid});
	 externalExamTypes.addData(data);
	 return externalExamTypes;
	 }
	 */
	public CommonBean findExternalExamTypeById(String id) {
		CommonBean externalExamTypes = this.createExternalExamType();
		String sql = "select * from T_EXTERNAL_EXAM_TYPE where id=?";
		List data = this.getCommonDao().select(sql, new String[] { id });
		externalExamTypes.addData(data);
		return externalExamTypes;
	}

	public CommonBean findAllExternalExamTypes() {
		CommonBean externalExamTypes = this.createExternalExamType();
		String sql = "select * from T_EXTERNAL_EXAM_TYPE ";
		List data = this.getCommonDao().select(sql, new String[] {});
		externalExamTypes.addData(data);
		return externalExamTypes;
	}

	public void insertExternalExamType(CommonBean externalExamTypeBean) {
		List data = externalExamTypeBean.getData();
		Row row = this.getRow(tableName);
		this.getCommonDao().templateBatchInsert(row, data, this.getIdGenerator());
	}

	public void updateExternalExamType(CommonBean externalExamTypeBean) {
		List data = externalExamTypeBean.getData();
		Row row = this.getRow(tableName);
		this.getCommonDao().templateBatchUpdate(row, data);
	}

	public CommonBean findExternalExamTypeByCode(String currentYear, String currentSemester, String currentType) {
		CommonBean externalExamTypes = this.createExternalExamType();
		String sql = "select * from T_EXTERNAL_EXAM_TYPE where school_year=? and school_semester=? and EXAM_TYPE=?";
		List data = this.getCommonDao().select(sql, new String[] { currentYear,currentSemester,currentType });
		externalExamTypes.addData(data);
		return externalExamTypes;
	}

	public void remove(CommonBean externalExamTypeBean) {
		if (externalExamTypeBean==null||externalExamTypeBean.getSize()==0) {
			return;
		}
		String sql="delete from T_EXTERNAL_EXAM_TYPE where school_year=? and school_semester=? and EXAM_TYPE=?";
		List params=new ArrayList();
		for (int i = 0; i < externalExamTypeBean.getSize(); i++) {
			String examType=externalExamTypeBean.getPropertyAsString(i, "examType");
			String schoolYear=externalExamTypeBean.getPropertyAsString(i, "schoolYear");
			String schoolSemester=externalExamTypeBean.getPropertyAsString(i, "schoolSemester");
			params.add( new String[]{schoolYear,schoolSemester,examType} );
		}
		getCommonDao().batchUpdateRecord(sql, params);
	}

	public CommonBean findEntryTypes(String currentYear, String currentSemester) {
		CommonBean externalExamTypes = this.createExternalExamType();
		String sql = "select * from T_EXTERNAL_EXAM_TYPE where school_year=? and school_semester=? and entry_status='1'  ";
		List data = this.getCommonDao().select(sql, new String[] { currentYear,currentSemester });
		externalExamTypes.addData(data);
		return externalExamTypes;
	}

	public void closeAllTypes(String year, String semester) {
		String sql="update T_EXTERNAL_EXAM_TYPE set  entry_status='0' where school_year=? and school_semester=? ";
		getCommonDao().updateRecord(sql, new String[]{year,semester});
		
	}
}
